using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;

namespace Sedna.Core.Controls
{
	[ValidationProperty("Text")]
	public class DateRange : BaseControl, INamingContainer, IClientScriptingControl
	{
		protected DatePicker datePickerFrom;
		protected DatePicker datePickerTo;

		
		private Boolean  selectHoliday = true;		

	    
	    public new IDateRangeConfiguration Configuration
	    {
            get { return (IDateRangeConfiguration) base.Configuration; }
            set { base.Configuration = value; }
	    }
	    
		protected override HtmlTextWriterTag TagKey
		{
			get { return HtmlTextWriterTag.Table; }
		}
		
		public bool SelectHoliday
		{
			get
			{
				return selectHoliday;
			}
			
			set
			{
				selectHoliday = value;
			}
		}		

		/// <summary>
		/// Gets or sets range beginning date (DateTime). Null if not set. 
		/// </summary>
		public object FromDate
		{
			get
			{
				EnsureChildControls();
				return datePickerFrom.Value;	
			}
			set
			{
                EnsureChildControls();
				datePickerFrom.Value=value;
			}
		}

		/// <summary>
		/// Gets or sets range ending date (DateTime). Null if not set. 
		/// </summary>
		public object ToDate
		{
			get
			{
				EnsureChildControls();
				return datePickerTo.Value;	
			}
			set
			{
                EnsureChildControls();
                datePickerTo.Value = value;
			}
		}

		public string Text
		{
			get
			{
				EnsureChildControls();
				if ((datePickerFrom.Text == null || datePickerFrom.Text == "")
					|| datePickerTo.Text == null || datePickerTo.Text == "")
				{
					return "";
				}
				return datePickerFrom.Text + "-" + datePickerTo.Text;
			}
		}

		public DateRange(IConfiguration configuration) : base (configuration)
		{
			Width=Unit.Pixel(200);
		}

		protected override void Build()
		{
			base.Build();
			IClientScriptingConfiguration clientScriptingConfiguration=configuration as IClientScriptingConfiguration;
			if (clientScriptingConfiguration==null)
			{
				return;

			}
			ClientScriptingEnabled=clientScriptingConfiguration.ClientScriptingEnabled;
			ClientVariable=clientScriptingConfiguration.ClientVariable;
		}


		protected override void CreateChildControls() 
		{	
            datePickerFrom = new DatePicker(Configuration.FromDateConfiguration);
            datePickerTo = new DatePicker(Configuration.ToDateConfiguration);
            datePickerTo.TimeOfDay = TimeSpan.FromDays(1) - TimeSpan.FromMilliseconds(1);


			TableRow row = new TableRow();
			Controls.Add(row);

			TableCell fromCell=new TableCell();
			fromCell.Width=Unit.Percentage(50);
			TableCell toCell=new TableCell();
			toCell.Width=Unit.Percentage(50);
			TableCell dashCell=new TableCell();
			dashCell.Width=Unit.Percentage(0);

			row.Controls.Add(fromCell);
			row.Controls.Add(dashCell);
			row.Controls.Add(toCell);

			dashCell.Controls.Add(new System.Web.UI.LiteralControl("<nobr>&nbsp;-&nbsp;</nobr>")) ;
	

			fromCell.Controls.Add(datePickerFrom);
			datePickerFrom.Width=Unit.Percentage(100);
			datePickerFrom.SelectHoliday = selectHoliday;
               
			toCell.Controls.Add(datePickerTo);
			datePickerTo.Width=Unit.Percentage(100);
			datePickerTo.SelectHoliday = selectHoliday;
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
            if (ClientScriptingEnabled)
            {
//				EnableClientScripting();
                DeclareScriptVariable();
            }
        }

        protected override void DeclareScriptVariable()
        {
            base.DeclareScriptVariable();
            datePickerFrom.ClientScriptingEnabled=true;
            datePickerTo.ClientScriptingEnabled=true;
        }
	    
		protected override void Unbind()
		{
            binder.Unbind(new string[] { "FromDate", "ToDate" }, configuration.DataParser);
            binder.Unbind(new string[] { "Enabled", "Text" });
		    
		}

		protected override void Bind()
		{
			binder.Bind(new string[]{"FromDate", "ToDate"}, configuration.DataTranslator);
            binder.Bind(new string[] { "Enabled", "SelectHoliday" });
		    
			Enabled = Enabled && configuration.IsEnabledForPrincipal();
        }

        #region Javascript support

	    protected override IDictionary BuildScriptParameters()
	    {
	        IDictionary parameters =  base.BuildScriptParameters();
	        parameters["beginDate"] = datePickerFrom.ClientVariable;
            parameters["endDate"] = datePickerTo.ClientVariable;
            return parameters;
	    }

	    #endregion


    }
}
